<?php
/**
 * GoSpecies
 * @link http://code.google.com/p/geocontexter/
 * @package GoSpecies
 */

/**
 * delete record
 *
 * @package GoSpecies
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author Armand Turpel <geocontexter@gmail.com>
 * @version $Rev: 153 $ / $LastChangedDate: 2010-02-18 17:21:41 +0100 (jeu., 18 févr. 2010) $ / $LastChangedBy: armand.turpel $
 */

namespace Gospecies\Controller;

use Zend\View\Model\ViewModel;
use Gospecies\Controller\AbstractIdentityJsonController;
use Zend\View\Model\JsonModel;

class AjaxRecordDeleteJsonController extends AbstractIdentityJsonController
{
    public function indexAction()
    {

        $params = array();

        try {

            // get logged user
            $this->user = $this->get_identity();

            $id_record  = $this->request->getPost()->id_record;

            if ($id_record !== null) {
                $params['id_record'] = $id_record;
            } else {
                return new JsonModel(array('error' => 'id_record not defined in AjaxRecordDeteleJsonController'));
            }

            $RecordGet = $this->CoreModel('RecordGet');

            $params    = array('id_record'       => (string)$id_record,
                               'geometry_column' => 'point');

            $record    = $RecordGet->run( $params );

            if ($record instanceof \Core\Library\Exception) {
                return new JsonModel(array('error' => $record->getTraceAsString()));
            }

            // check permission if id_group is "contributor" or above.
            // users of those groups can only delete own records
            //
            if ((isset($this->user->id_group) && ((int)$this->user->id_status == 200))) {
                if ((int)$this->user->id_group >= 300) {
                    if ($this->user->id_user !== $record['id_owner']) {
                        return new JsonModel(array('error' => 'permission denied'));
                    }
                }
            } else {
                return new JsonModel(array('error' => 'permission denied'));
            }

            $RecordDelete = $this->CoreModel('RecordDelete');

            $result  = $RecordDelete->run( $params );

            if ($result instanceof \Core\Library\Exception) {
                return new JsonModel(array('error' => $result->getTraceAsString()));
            }


            $_dir  = realpath(GEOCONTEXTER_ROOT . '/public/data/gc_record/' . $id_record . '_' . $record['media_folder_hash']);
            $this->recursiveRmDir( $_dir );


            return new JsonModel(array('data' => 'Delete of id_record: ' . $id_record . ' succeed.'));

          } catch (\Exception $e) {

            $this->default_error = "Error\n File: " . __file__ . "\nLine: " . __line__ . "\nError: " . $e->getTraceAsString();

            return new JsonModel(array('error' => $this->default_error));

          }

    }

    function recursiveRmDir($dir)
    {
        $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST);
        foreach ($iterator as $filename => $fileInfo) {
            if ($fileInfo->isDir()) {
                if (false === rmdir($filename)) {
                    $this->service->get('CoreErrorLogger')->info('Error delete directory: ' . $dir . '/' . $filename);
                }
            } else {
                if (false === unlink($filename)) {
                    $this->service->get('CoreErrorLogger')->info('Error delete file: ' . $dir . '/' . $filename);
                }
            }
        }

        if (false === rmdir($dir)) {
            $this->service->get('CoreErrorLogger')->info('Error delete directory: ' . $dir);
        }

    }
}

